﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.usercontrols;
using Cashier_Utils;
using Cashier.utils;
using Cashier_Model;
using System.Text.RegularExpressions;

namespace Cashier.pos
{
    public partial class PayForm : Form
    {
        private PosForm posform;
        /// <summary>
        /// 帐单金额
        /// </summary>
        private Decimal shipmentTotal = 0M;
        /// <summary>
        /// 卡内余额
        /// </summary>
        private Decimal balance = 0M;
        /// <summary>
        /// 使用卡内金额
        /// </summary>
        private Decimal usedBalance = 0M;   
        /// <summary>
        /// 应付金额
        /// </summary>
        private Decimal payableAmount = 0M;    
        /// <summary>
        /// 会员拥有积分数
        /// </summary>
        private Int32 memberIntegral = 0;       
        /// <summary>
        /// 使用的积分兑换金额
        /// </summary>
        private Decimal integralAmount = 0M;
        /// <summary>
        /// 使用积分数
        /// </summary>
        private Int32 payIntegral = 0;          
        private bool isPay = false;
        private Member member;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="posform"></param>
        /// <param name="shipmentTotal"></param>
        /// <param name="member"></param>
        public PayForm(PosForm posform, Decimal shipmentTotal, Member member)
        {
            InitializeComponent();
            this.posform = posform;
            this.shipmentTotal = shipmentTotal;
            this.member = member;
            this.payableAmount = shipmentTotal;

            textbox_total.ForeColor = Color.Red;
            textbox_shipmentTotal.Text = shipmentTotal.ToString("f2");
            textbox_total.Text = shipmentTotal.ToString("f2");

            if (member != null)
            {
                if (member.CurrentIntegral > 0)
                {
                    memberIntegral = member.CurrentIntegral;
                    label_integral.Text = memberIntegral.ToString();
                    /*
                    if (Config.ISPAY)
                    {
                        Decimal tempIntegralAmount = ToolsUtil.IntegralExchangeMoney(memberIntegral);
                        if (Decimal.Compare(tempIntegralAmount,shipmentTotal) <= 0)
                        {
                            integralAmount = tempIntegralAmount;
                        }
                        else
                        {
                            integralAmount = shipmentTotal;
                        }
                        textbox_integralAmount.Text = integralAmount.ToString("f2");
                        payableAmount = Decimal.Subtract(shipmentTotal, integralAmount);
                    }
                     * */
                }
                /*
                if (member.Balance > 0)
                {
                    balance = member.Balance;
                    //textbox_usedBalance.Enabled = true;
                    textbox_balance.Text = balance.ToString("f2");
                    //textbox_usedBalance.LostFocus += new EventHandler(textbox_usedBalance_LostFocus);
                    Decimal tempAmount = Decimal.Subtract(shipmentTotal, integralAmount);
                    if (tempAmount > 0)
                    {
                        if (Decimal.Compare(balance, tempAmount) <= 0)
                        {
                            usedBalance = balance;
                        }
                        else
                        {
                            usedBalance = tempAmount;
                        }
                        textbox_usedBalance.Text = usedBalance.ToString("f2");
                    }
                }
                payableAmount = Decimal.Subtract(payableAmount, usedBalance);
                textbox_total.Text = payableAmount.ToString("f2");
                if (payableAmount == 0)
                {
                    textbox_pay.Enabled = false;
                    textbox_give.Enabled = false;
                }
                 * */
                this.UsedIntegralAmount(0M);
            }
            else
            {
                textbox_usedBalance.Enabled = false;
            }
        }

        /// <summary>
        /// 计算使用积分金额
        /// </summary>
        /// <param name="usedAmount"></param>
        public void UsedIntegralAmount(Decimal usedAmount)
        {
            this.integralAmount = usedAmount;
            textbox_integralAmount.Text = integralAmount.ToString("f2");
            payableAmount = Decimal.Subtract(shipmentTotal, integralAmount);
            if (member.Balance > 0)
            {
                balance = member.Balance;
                //textbox_usedBalance.Enabled = true;
                textbox_balance.Text = balance.ToString("f2");
                //textbox_usedBalance.LostFocus += new EventHandler(textbox_usedBalance_LostFocus);
                Decimal tempAmount = Decimal.Subtract(shipmentTotal, integralAmount);
                if (tempAmount > 0)
                {
                    if (Decimal.Compare(balance, tempAmount) <= 0)
                    {
                        usedBalance = balance;
                    }
                    else
                    {
                        usedBalance = tempAmount;
                    }
                    textbox_usedBalance.Text = usedBalance.ToString("f2");
                }
            }
            payableAmount = Decimal.Subtract(payableAmount, usedBalance);
            textbox_total.Text = payableAmount.ToString("f2");
            if (payableAmount == 0)
            {
                textbox_pay.Enabled = false;
                textbox_give.Enabled = false;
            }
        }

        /// <summary>
        /// 确定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender, EventArgs e)
        {
            isPay = true;
            this.button_ok.Enabled = false;
            this.button_cancel.Enabled = false;
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            isPay = false;
            this.Close();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PayForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isPay)
            {              
                if (textbox_pay.Ispassed && textbox_give.Ispassed)
                {
                    String s_integralAmount = textbox_integralAmount.Text.Trim();
                    if (String.IsNullOrEmpty(s_integralAmount) || !Regex.IsMatch(s_integralAmount, RegexExpress.DECIMAL_TWO_BITS))
                    {
                        s_integralAmount = "0";
                    }
                    integralAmount = Decimal.Parse(s_integralAmount);

                    String s_usedBalance = textbox_usedBalance.Text.Trim();
                    if (String.IsNullOrEmpty(s_usedBalance) || !Regex.IsMatch(s_usedBalance, RegexExpress.DECIMAL_TWO_BITS))
                    {
                        s_usedBalance = "0";
                    }
                    usedBalance = Decimal.Parse(s_usedBalance);

                    String s_total = textbox_total.Text.Trim();
                    if (String.IsNullOrEmpty(s_total) || !Regex.IsMatch(s_total, RegexExpress.DECIMAL_TWO_BITS))
                    {
                        s_total = "0";
                    }
                    payableAmount = Decimal.Parse(s_total);

                    String s_pay = textbox_pay.Text.Trim();
                    if (String.IsNullOrEmpty(s_pay) || !Regex.IsMatch(s_pay, RegexExpress.DECIMAL_TWO_BITS))
                    {
                        s_pay = "0";
                    }
                    decimal payAmount = Decimal.Parse(s_pay);

                    String s_give = textbox_give.Text.Trim();
                    if (String.IsNullOrEmpty(s_give) || !Regex.IsMatch(s_give, RegexExpress.DECIMAL_TWO_BITS))
                    {
                        s_give = "0";
                    }
                    decimal giveAmount = Decimal.Parse(s_give);

                    if (usedBalance > balance)
                    {
                        MessageBox.Show("卡内余额不足！");
                        textbox_usedBalance.Focus();
                        e.Cancel = true;
                        return;
                    }
                    if (usedBalance > (shipmentTotal-integralAmount))
                    {
                        MessageBox.Show("支付金额超过帐单金额！");
                        textbox_usedBalance.Focus();
                        e.Cancel = true;
                        return;
                    }
                    if (payableAmount != (shipmentTotal - integralAmount - usedBalance))
                    {
                        MessageBox.Show("卡内余额支付有误！");
                        textbox_usedBalance.Focus();
                        e.Cancel = true;
                        return;
                    }
                    if ((payAmount - giveAmount) > payableAmount)
                    {
                        MessageBox.Show("支付金额有误！");
                        textbox_pay.Focus();
                        e.Cancel = true;
                        return;
                    }

                    PayDataInfo payDataInfo = new PayDataInfo();
                    payDataInfo.ShipmentState = ShipmentState.SAVE;
                    payDataInfo.Payway = Payway.Cash;
                    payDataInfo.UsedBalance = usedBalance;
                    payDataInfo.PayableAmount = payableAmount;
                    payDataInfo.PayAmount = payAmount;
                    payDataInfo.GiveAmount = giveAmount;
                    payDataInfo.IntegralAmount = integralAmount;
                    decimal debt = payAmount - payableAmount;
                    if (Config.IS_POS_DEBT) //是否允许欠款
                    { 
                        if (debt < 0)
                        {
                            if (!String.IsNullOrEmpty(posform.Membername))
                            {
                                //结帐
                                posform.ShipmentAccounts(payDataInfo);
                            }
                            else
                            {
                                MessageBox.Show("只有会员才能享受帐单分期付款！");
                                e.Cancel = true;
                            }
                        }
                        else
                        {
                            //结帐
                            posform.ShipmentAccounts(payDataInfo);
                        }
                    }
                    else
                    {
                        if (payAmount >= payableAmount)
                        {
                            //结帐
                            posform.ShipmentAccounts(payDataInfo);
                        }
                        else
                        {
                            MessageBox.Show("付款金额不得低于应付金额！");
                            textbox_pay.Focus();
                            e.Cancel = true;
                        }
                    }
                }
                else
                {
                    if (!textbox_pay.Ispassed)
                        textbox_pay.Focus();
                    else if (!textbox_give.Ispassed)
                        textbox_give.Focus();
                    e.Cancel = true;
                }
            }
        }

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            if (keyData == Keys.Escape)
            {
                isPay = false;
                this.Close();
            }
            else if (keyData == Keys.Enter)
            {
                if (!textbox_usedBalance.Focused && !textbox_pay.Focused && !textbox_give.Focused && !button_ok.Focused)
                {
                    isPay = true;
                    this.Close();
                }
            }
            else if (keyData == Keys.D)
            {
                if (Config.ISPAY && member!=null && member.Id>0)
                {
                    IntegralForm integralForm = new IntegralForm(this, member.CurrentIntegral, shipmentTotal);
                    integralForm.ShowDialog();
                }
            }
            return b;
        }

        /// <summary>
        /// 余额支付框失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_usedBalance_LostFocus(Object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textbox_usedBalance.Text.Trim()))
            {
                usedBalance = Decimal.Parse(textbox_usedBalance.Text.Trim());
                if (usedBalance > balance)
                {
                    MessageBox.Show("使用余额不得超过卡内金额！");
                    textbox_usedBalance.Text = "0.00";
                    usedBalance = 0;
                    textbox_usedBalance.Focus();
                }
                else
                {
                    if (usedBalance > shipmentTotal)
                    {
                        MessageBox.Show("使用余额超过应付金额！");
                        textbox_usedBalance.Text = "0.00";
                        usedBalance = 0;
                        textbox_usedBalance.Focus();
                    }
                    else
                    {
                        payableAmount = shipmentTotal - usedBalance;
                        textbox_total.Text = payableAmount.ToString("f2");
                    }
                }
            }
        }

        /// <summary>
        /// 付款框失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void textbox_pay_LostFocus(Object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textbox_pay.Text.Trim()))
            {
                textbox_pay.Text = "0.00";
            }
            else
            {
                decimal payAmount = Decimal.Parse(textbox_pay.Text.Trim());               
                textbox_pay.Text = payAmount.ToString("f2");
                payableAmount = Decimal.Parse(textbox_total.Text.Trim());
                if (payAmount >= payableAmount)
                    textbox_give.Text = (payAmount - payableAmount).ToString("f2");
                else
                    textbox_give.Text = "0.00";
            }
        }

    }
}
